package user

import (
	"github.com/gin-gonic/gin"
	user3 "server/app/api/request/user"
	"server/base"
	user2 "server/common/service/user"
	"server/pkg/response"
)

var Auth = auth{}

type auth struct{}

// @Summary 用户登录
// @Description 用户登录
// @Tags 用户
// @Accept  json
// @Produce  json
// @Param code query string true "code"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/mine/login [post]
func (*auth) Login(c *gin.Context) {
	var req user3.LoginReq
	err := c.ShouldBind(&req)
	if err != nil {
		response.Error(c, err.Error())
	}

	// 获取openid
	openId, _ := base.App.WxTools.Code2Session(req.Code)
	if openId == "" {
		response.Error(c, "登录失败")
	}

	// 登录
	userAuth, token, err := user2.AuthService.Login(openId)
	if err != nil {
		response.Error(c, err.Error())
	}

	response.Success(c, gin.H{"token": token, "status": userAuth.Status, "user_id": userAuth.Id})
}
